/*
 * 描          述:  <描述>
 * 修  改   人:  Administrator
 * 修改时间:  2017年2月15日
 * <修改描述:>
 */
package com.tx.component.file.util;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.util.StringUtils;

import com.tx.component.file.catalog.VitualFileCatalog;
import com.tx.component.file.resource.FCResource;
import com.tx.component.file.resource.FCResourceDetail;
import com.tx.core.TxConstants;
import com.tx.core.exceptions.util.AssertUtils;

/**
 * 客户信息文件辅助类<br/>
 * <功能详细描述>
 * 
 * @author  Administrator
 * @version  [版本号, 2017年2月15日]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
public abstract class FileContextUtils {
    
    /**
     * 获取默认的对外呈现Url<br/>
     * <功能详细描述>
     * @param catalog
     * @param relativePath
     * @return [参数说明]
     * 
     * @return String [返回类型说明]
     * @exception throws [异常类型] [异常说明]
     * @see [类、类#方法、类#成员]
     */
    public static String defaultViewUrl(String catalog, String relativePath) {
        AssertUtils.notEmpty(catalog, "catalog is empty.");
        AssertUtils.notEmpty(relativePath, "relativePath is empty.");
        
        StringBuilder sb = new StringBuilder(TxConstants.INITIAL_STR_LENGTH);
        sb.append("/fc/").append(catalog).append("/view/").append(relativePath);
        return sb.toString();
    }
    
    /**
     * 处理文件保存路径,去除相对路径前面的"/",相对路径应以"."或"字符"作为起始字符<br/>
     * <功能详细描述>
     * @param relativePath
     * @return [参数说明]
     * 
     * @return String [返回类型说明]
     * @exception throws [异常类型] [异常说明]
     * @see [类、类#方法、类#成员]
     */
    public static String handleRelativePath(String relativePath) {
        AssertUtils.notEmpty(relativePath, "relativePath is empty.");
        
        relativePath = StringUtils.cleanPath(relativePath);
        //整理存储的relativePath相对路径
        if (relativePath.startsWith("/")) {
            //去除相对路径前面的"/"
            relativePath = relativePath.substring(1, relativePath.length());
            return handleRelativePath(relativePath);
        }
        //不能以..作为开头
        if (relativePath.startsWith(".")) {
            //去除相对路径前面的"/"
            relativePath = relativePath.substring(1, relativePath.length());
            return handleRelativePath(relativePath);
        }
        //不能以"/"作为结尾
        while (relativePath.endsWith("/")) {
            //去除相对路径前面的"/"
            relativePath = relativePath.substring(0, relativePath.length() - 1);
        }
        
        return relativePath;
    }
    
    /**
     * 生成资源详情<br/>
     * <功能详细描述>
     * @param vfc
     * @param res
     * @return [参数说明]
     * 
     * @return FCResourceDetail [返回类型说明]
     * @exception throws [异常类型] [异常说明]
     * @see [类、类#方法、类#成员]
     */
    public static FCResourceDetail wrap(VitualFileCatalog vfc, FCResource res) {
        AssertUtils.notNull(vfc, "vitualFileCatalog is null.");
        AssertUtils.notNull(res, "resource is null.");
        
        FCResourceDetail detail = new FCResourceDetail(vfc, res);
        return detail;
    }
    
    /**
     * 生成资源详情<br/>
     * <功能详细描述>
     * @param vfc
     * @param res
     * @return [参数说明]
     * 
     * @return FCResourceDetail [返回类型说明]
     * @exception throws [异常类型] [异常说明]
     * @see [类、类#方法、类#成员]
     */
    public static List<FCResourceDetail> wrap(VitualFileCatalog vfc,
            List<FCResource> resourceList) {
        AssertUtils.notNull(vfc, "vitualFileCatalog is null.");
        List<FCResourceDetail> resList = new ArrayList<>();
        if (CollectionUtils.isEmpty(resList)) {
            return resList;
        }
        resList = resourceList.stream()
                .map(res -> new FCResourceDetail(vfc, res))
                .collect(Collectors.toList());
        return resList;
    }
    
    public static void main(String[] args) {
        String test = ".././33234/2342/23434/../../23424///";
        System.out.println(test.substring(0, test.length() - 1));
        System.out.println(FileContextUtils.handleRelativePath(test));
    }
    
}
